package com.xszx.order.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.order.client.EduClient;
import com.xszx.order.client.UcenterClient;
import com.xszx.order.entity.Order;
import com.xszx.order.mapper.OrderMapper;
import com.xszx.order.service.OrderService;
import com.xszx.order.util.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 白熊
 * @since 2024-07-26
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);

        //远程调用 service-edu 模块 根据课程id查询课程信息
        Object item = eduClient.getById(courseId).getData().get("item");
        //利用hutool将查询到的信息转换为json字符串
        String s = JSONUtil.toJsonStr(item);
        Map map = JSONUtil.toBean(s, Map.class);//转换成Map集合
        order.setCourseTitle(map.get("title").toString());
        order.setCourseCover(map.get("cover").toString());
        order.setTotalFee(new BigDecimal(map.get("price").toString()));

        //远程调用 service-edu 模块 根据讲师id调用讲师信息
        Map teacher = (Map) eduClient.getById1(map.get("teacherId").toString()).getData().get("item");
        order.setTeacherName(teacher.get("name").toString());

        //远程调用 service-ucenter 模块 根据memberID查询客户信息
        Map member = (Map) ucenterClient.getInfo(memberId).getData().get("item");
        order.setMemberId(memberId);
        order.setNickname(member.get("nickname").toString());
        order.setMobile(member.get("mobile").toString());

        //设置其他属性
        order.setPayType(1);
        order.setStatus(0);

        baseMapper.insert(order);
        return order.getOrderNo();//返回的订单号
    }
}
